package ace.cmp.kaptcha.core.properties;

import ace.cmp.kaptcha.core.enums.ObscurificatorEnum;
import java.awt.*;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.NestedConfigurationProperty;

/**
 * @author caspar
 * @date 2023/3/21 15:17
 */
@ConfigurationProperties(prefix = KaptchaProperties.CONFIG_PREFIX)
@Data
public class KaptchaProperties {
  public static final String CONFIG_PREFIX = "ace.captcha.module.kaptcha";
  public static final String DEFAULT_NOISE_IMPLEMENT = "com.google.code.kaptcha.impl.DefaultNoise";

  /**
   * 宽度
   */
  private Integer width = 200;

  /**
   * 高度
   */
  private Integer height = 50;

  /**
   * 干扰器
   */
  private Noise noise = new Noise();

  /**
   * 边框
   */
  @NestedConfigurationProperty private Border border = new Border();

  /**
   * 内容
   */
  @NestedConfigurationProperty private TextProducer textProducer = new TextProducer();

  /**
   * 背景色
   */
  @NestedConfigurationProperty private Background background = new Background();

  /**
   * 图片样式
   * 水纹 com.google.code.kaptcha.impl.WaterRipple
   * 鱼眼 com.google.code.kaptcha.impl.FishEyeGimpy
   * 阴影 com.google.code.kaptcha.impl.ShadowGimpy
   */
  private ObscurificatorEnum obscurificatorImpl = ObscurificatorEnum.SHADOW_GIMPY;

  /**
   * 图片实现类
   */
  private String producerImpl = "com.google.code.kaptcha.impl.DefaultKaptcha";

  /**
   * 文字实现器
   */
  private String wordImpl = "com.google.code.kaptcha.text.impl.DefaultWordRenderer";

  @Data
  @Builder
  @NoArgsConstructor
  @AllArgsConstructor
  public static class TextProducer {
    /**
     * 验证码字符实现器
     */
    private String impl = "com.google.code.kaptcha.text.impl.DefaultTextCreator";

    /**
     * 验证码字符串规则
     */
    private CharConfig charConfig = new CharConfig();

    /**
     * 字体配置
     */
    private FontConfig fontConfig = new FontConfig();

    @Data
    @Builder
    @NoArgsConstructor
    @AllArgsConstructor
    public static class FontConfig {
      /**
       * 字体名
       */
      private String names = "Arial,Courier";

      /**
       * 字体颜色
       */
      private Color color = Color.BLACK;

      /**
       * 字体尺寸
       */
      private Integer size = 40;
    }

    @Data
    @Builder
    @NoArgsConstructor
    @AllArgsConstructor
    public static class CharConfig {

      /**
       * 内容源
       */
      private String source = "abcdefghjklmnopqrstuvwxyz23456789";

      /**
       * 内容长度
       */
      private Integer length = 4;

      /**
       * 内容间隔
       */
      private Integer space = 2;
    }
  }

  @Data
  @Builder
  @NoArgsConstructor
  @AllArgsConstructor
  public static class Border {

    /**
     * 是否开启
     */
    private Boolean enabled = true;

    /**
     * 颜色
     */
    private Color color = Color.BLACK;

    /**
     * 厚度
     */
    private Integer thickness = 1;
  }

  @Data
  @Builder
  @NoArgsConstructor
  @AllArgsConstructor
  public static class Noise {
    /**
     * 干扰颜色
     */
    private Color color = Color.BLACK;

    /**
     * 干扰器实现类
     */
    private String impl = DEFAULT_NOISE_IMPLEMENT;
  }

  /**
   * 背景
   */
  @Data
  @Builder
  @NoArgsConstructor
  @AllArgsConstructor
  public static class Background {
    /**
     * 背景实现
     */
    private String impl = "com.google.code.kaptcha.impl.DefaultBackground";

    /**
     * 背景渐变色
     */
    private Clear clear = new Clear();

    @Data
    @Builder
    @NoArgsConstructor
    @AllArgsConstructor
    public static class Clear {
      /**
       * 开始渐变色
       */
      private Color from = Color.LIGHT_GRAY;

      /**
       * 结束渐变色
       */
      private Color to = Color.WHITE;
    }
  }
}
